<!-- +----------------------------------------------------------------------
| 麦沃德科技赋能开发者，助力中小企业发展 
+----------------------------------------------------------------------
| Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
+----------------------------------------------------------------------
| 沃德代驾系统并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
+----------------------------------------------------------------------
| Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
+----------------------------------------------------------------------
| 组件-省市区选择器 开发者: 麦沃德科技-半夏
+---------------------------------------------------------------------- -->

<template>
	<view class="component-modal-address" @click.stop>
		<uni-popup ref="popupModal" type="bottom" :safe-area="false" @change="onChange">
			<view class="modal-box" :style="{'--theme-color': themeColor}">
				<view class="modal-head">
					<view class="head-title">地址选择</view>
					<view class="head-btn" @click="handleConfirm">确认</view>
				</view>
				<view class="modal-content flex align-items-center">
					<picker-view class="content-picker" indicator-style="height: 70rpx;" :value="selectValue" @change="handleChange">
						<picker-view-column>
							<view class="picker-column" v-for="(item, index) in provinceList" :key="index">{{item}}</view>
						</picker-view-column>
						<picker-view-column>
							<view class="picker-column" v-for="(item, index) in cityList" :key="index">{{item.city}}</view>
						</picker-view-column>
						<picker-view-column>
							<view class="picker-column" v-for="(item, index) in areaList" :key="index">{{item}}</view>
						</picker-view-column>
					</picker-view>
				</view>
				<view class="modal-btn" @click="onClose">取消</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	export default {
		name: "componentModalAddress",
		data() {
			return {
				// 省份列表
				provinceList: [],
				// 城市列表
				cityList: [],
				// 地区列表
				areaList: [],
				// 已选地址
				addressList: [],
				// 已选数据
				selectValue: [0, 0, 0],
				// 参数
				parameter: "",
			};
		},
		computed: {
			...mapState({
				themeColor: state => state.app.themeColor,
			})
		},
		mounted() {
			this.getProvinceList()
		},
		methods: {
			// 打开模态框
			open(value, parameter) {
				const list = value?.split("/") || []
				this.addressList = list
				this.parameter = parameter
				if (this.provinceList.length) {
					this.setValueChange()
				} else {
					this.getProvinceList(() => {
						this.setValueChange()
					})
				}
				this.$refs.popupModal.open()
			},
			// 关闭弹窗
			onClose() {
				this.$refs.popupModal.close()
			},
			// 改变事件
			onChange(e) {
				this.$emit("onChange", e.show)
			},
			// 数值改变
			async handleChange(e) {
				if (this.selectValue[0] != e.target.value[0]) {
					this.selectValue = [e.target.value[0], 0, 0]
					this.cityList = await this.getCityList(this.provinceList[this.selectValue[0]])
					this.areaList = await this.getAreaList(this.cityList[this.selectValue[1]].city)
				} else if (this.selectValue[1] != e.target.value[1]) {
					this.selectValue = [e.target.value[0], e.target.value[1], 0]
					this.areaList = await this.getAreaList(this.cityList[this.selectValue[1]].city)
				} else {
					this.selectValue = e.target.value
				}
			},
			// 设置选中数值
			async setValueChange() {
				var selectData = [0, 0, 0]
				if (this.addressList[0]) {
					for (var i = 0; i < this.provinceList.length; i++) {
						if (this.provinceList[i] == this.addressList[0]) {
							selectData[0] = i
							break
						}
					}
				}
				this.cityList = await this.getCityList(this.provinceList[selectData[0]])
				if (this.addressList[1]) {
					for (var i = 0; i < this.cityList.length; i++) {
						if (this.cityList[i].city == this.addressList[1]) {
							selectData[1] = i
							break
						}
					}
				}
				this.areaList = await this.getAreaList(this.cityList[selectData[1]].city)
				if (this.addressList[2]) {
					for (var i = 0; i < this.areaList.length; i++) {
						if (this.areaList[i] == this.addressList[2]) {
							selectData[2] = i
							break
						}
					}
				}
				this.$nextTick(() => {
					this.selectValue = selectData
					this.$forceUpdate()
				})
			},
			// 确认
			handleConfirm() {
				var data = {
					province: this.provinceList[this.selectValue[0]],
					city: this.cityList[this.selectValue[1]].city,
					area: this.areaList[this.selectValue[2]],
				}
				this.$emit("confirm", data, this.parameter)
				this.onClose()
			},
			// 获取省份数据
			getProvinceList(fn) {
				this.$util.request("driver.address.province").then(res => {
					if (res.code == 1) {
						this.provinceList = res.data
						if (fn) fn()
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					console.error('获取省份数据 ', error)
				})
			},
			// 获取城市数据
			getCityList(name) {
				return new Promise((resolve, reject) => {
					this.$util.request("driver.address.city", {
						province: name
					}).then(res => {
						if (res.code == 1) {
							resolve(res.data)
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
							reject(false)
						}
					}).catch(error => {
						console.error('获取城市数据 ', error)
						reject(false)
					})
				})
			},
			// 获取地区数据
			getAreaList(name) {
				return new Promise((resolve, reject) => {
					this.$util.request("driver.address.area", {
						city: name
					}).then(res => {
						if (res.code == 1) {
							if (res.data && res.data.length) {
								resolve(res.data)
							} else {
								resolve(["全部"])
							}
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
							reject(false)
						}
					}).catch(error => {
						console.error('获取省份数据 ', error)
						reject(false)
					})
				})
			},
		},
	}
</script>

<style lang="scss" scoped>
	.component-modal-address {
		position: relative;
		z-index: 999;

		.modal-box {
			background: #FFFFFF;
			border-radius: 20rpx 20rpx 0 0;
			width: 100vw;
			padding-bottom: constant(safe-area-inset-bottom);
			padding-bottom: env(safe-area-inset-bottom);

			.modal-head {
				display: flex;
				align-items: center;
				padding: 32rpx;

				.head-title {
					flex: 1;
					color: #333;
					text-align: center;
					font-size: 32rpx;
					font-weight: 600;
					line-height: 44rpx;
					padding-left: 128rpx;
				}

				.head-btn {
					color: #FFF;
					font-size: 28rpx;
					line-height: 40rpx;
					padding: 12rpx 36rpx;
					border-radius: 10rpx;
					background: var(--theme-color);
				}
			}

			.modal-content {
				padding-bottom: 32rpx;

				.content-picker {
					height: 400rpx;
					flex: 1;

					.picker-column {
						line-height: 70rpx;
						text-align: center;
						font-size: 28rpx;
					}
				}
			}

			.modal-btn {
				color: #E10602;
				text-align: center;
				font-size: 32rpx;
				line-height: 44rpx;
				padding: 32rpx;
			}
		}
	}
</style>